package com.hyb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyb.pojo.Bigitems;
import com.hyb.pojo.Smallitems;
import com.hyb.service.BigitemsService;
import com.hyb.service.SmallitemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class NavController {
    @Autowired
    BigitemsService bigitemsService;
    @Autowired
    SmallitemsService smallitemsService;
    @RequestMapping("/nav-list")
    public String toNavList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,ModelMap map){
        PageHelper.startPage(pageNum,10);
        List<Bigitems> BigList = bigitemsService.getAll();
        List<Smallitems> SmallList = smallitemsService.getAll();
        PageInfo<Bigitems> pageInfo = new PageInfo<>(BigList);
        map.addAttribute("pageInfo",pageInfo);
        map.addAttribute("SmallList",SmallList);
        return "/admin/nav-list";
    }
    @RequestMapping("/addBigItems")
    @ResponseBody
    public String addBigItems(@RequestParam Map data){
        String msg= "";
        System.out.println(data);
        List<Bigitems> all = bigitemsService.getAll();
        for (Bigitems bigitems : all) {
            if (bigitems.getBi_name().equals(data.get("bi_name"))){
                msg="此导航栏已存在";
                return msg;
            }
        }
        msg="ok";
        bigitemsService.addAll((String)data.get("bi_name"));
        return msg;
    }
    @RequestMapping("/delBigItems")
    public String delBigItems(@RequestParam int id){
        List<Smallitems> byBisId = smallitemsService.getByBisId(id);
        for (Smallitems smallitems : byBisId) {
            smallitemsService.deleteById(smallitems.getSi_id());
        }
        bigitemsService.deleteById(id);
        return "redirect:/nav-list";
    }
    @RequestMapping("/delSelectBig")
    @ResponseBody
    public String delSelectBig(@RequestParam int[] data){
        for (int datum : data) {
            bigitemsService.deleteById(datum);
        }
        return"ok";
    }
    @RequestMapping("/delSmallItems")
    @ResponseBody
    public String delSmallItems(@RequestParam int id){
        smallitemsService.deleteById(id);
        return "ok";
    }
    @RequestMapping("/delSelectSmall")
    @ResponseBody
    public String delSelectSmall(@RequestParam int[] data){
        for (int datum : data) {
           smallitemsService.deleteById(datum);
        }
        return"ok";
    }
    @RequestMapping("/toAddSmallItems")
    public String toAddSmallItems(@RequestParam Integer id,ModelMap map){
        Bigitems bigItems = bigitemsService.getById(id);
        map.addAttribute("bigItems",bigItems);
        return"/admin/smallItems-add";
    }
    @RequestMapping("/addSmallItems")
    @ResponseBody
    public String addSmallItems(@RequestParam Map data){
        String msg="";
        List<Smallitems> all = smallitemsService.getByBisId(Integer.parseInt((String)data.get("bis_id")));
        for (Smallitems smallitems : all) {
            if(smallitems.getSi_name().equals(data.get("si_name"))){
                msg="此导航栏下的子项目已存在";
                return msg;
            }
        }
        smallitemsService.addAll(data);
        msg="ok";
        return msg;
    }
    @RequestMapping("/toEditSmallItems")
    public String toEditSmallItems(@RequestParam Integer si_id,@RequestParam Integer bis_id,ModelMap map){
        Bigitems bigItems = bigitemsService.getById(bis_id);
        Smallitems smallItems = smallitemsService.getById(si_id);
        map.addAttribute("bigItems",bigItems);
        map.addAttribute("smallItems",smallItems);
        return"/admin/smallItems-edit";
    }
    @RequestMapping("/editSmallItems")
    @ResponseBody
    public String editSmallItems(@RequestParam Map data){
        String msg="";
        List<Smallitems> list= smallitemsService.getByBisIdExcludeById(data);
        for (Smallitems smallitems : list) {
            if (smallitems.getSi_name().equals(data.get("si_name"))){
                msg="导航栏下的此子项目已存在";
                return msg;
            }
        }
        msg="ok";
        smallitemsService.updateById(data);
        return msg;
    }
    @RequestMapping("/toEditBigItems")
    public String toEditBigItems(@RequestParam Integer id,ModelMap map){
        Bigitems bigItems = bigitemsService.getById(id);
        map.addAttribute("bigItems",bigItems);
        return"/admin/bigItems-edit";
    }
    @RequestMapping("/editBigItems")
    @ResponseBody
    public String editBigItems(@RequestParam Map data){
        String msg="";
        List<Bigitems> list= bigitemsService.getAllexcludeById(Integer.parseInt((String)data.get("bi_id")));
        System.out.println(list);
        for (Bigitems bigitems : list) {
            if (bigitems.getBi_name().equals(data.get("bi_name"))){
                msg="此导航栏已存在";
                return msg;
            }
        }
        msg="ok";
        bigitemsService.updateById(data);
        return msg;
    }
    @RequestMapping("/updateSort")
    @ResponseBody
    public String updateSort(@RequestParam Map data){
        System.out.println(data);
        Bigitems bigitems = bigitemsService.getById(Integer.parseInt((String)data.get("bi_id")));
        System.out.println(bigitems.getBi_name());
        data.put("bi_name",bigitems.getBi_name());
        System.out.println(data);
        bigitemsService.updateById(data);
        return "ok";
    }
}
